#include <stdio.h>
#include <string>
#include "main.h"
#include "stringconv.h"
#include <iostream>

struct testStru
{
    enum class insideEnum
    {
        a,
        x
    };
    insideEnum tt;
};

struct mesh
{
    int vCount;
    int iCount;
};

stringAct strFun;
void *c_h_void;
mesh *c_h_mesh;
int *c_h_int;

EXPORT_API void srt2CharPtr(std::string str, char *buf_arr)
{
    for (int i = 0, len = str.length(); i < len; i++)
    {
        buf_arr[i * 2] = str[i];
    }
}

EXPORT_API void regStrAct(stringAct val)
{
    strFun = val;
}

//参数 字符串
EXPORT_API void pString(char16_t *val , char *buf_arr)
{
    std::string str = "pString=";
    str.append(StringConv::ToUtf8String(val));
    if (strFun != nullptr)
    {
        srt2CharPtr(str, buf_arr);
        strFun((char16_t *)buf_arr);
    }
}

//返回值 字符串
EXPORT_API char16_t *retString(char *buf_arr)
{
    std::string str = "retStrOK";
    srt2CharPtr(str, buf_arr);
    return (char16_t *)buf_arr;
}

//参数 handle_void
EXPORT_API void pHVoid(void *val, char *buf_arr)
{
    int *arr = (int *)val;
    std::string str = "";
    str.append(std::to_string(arr[0]));
    // str.append(std::to_string((unsigned long)buf_arr));
    if (strFun != nullptr)
    {
        srt2CharPtr(str, buf_arr);
        strFun((char16_t *)buf_arr);
    }
}

//返回值 handle_void
EXPORT_API void *retHVoid(int *xxx_arr)
{
    return xxx_arr;
}

//参数 空结构Handel
EXPORT_API void pHStruct(mesh *val, char *buf_arr)
{
    std::string str = "pHStruct=";
    str.append(std::to_string(val->vCount));
    str.append(",");
    str.append(std::to_string(val->iCount));
    if (strFun != nullptr)
    {
        srt2CharPtr(str, buf_arr);
        strFun((char16_t *)buf_arr);
    }
    c_h_mesh = val;
}

//返回值 空结构Handel
EXPORT_API mesh *retHStruct()
{
    c_h_mesh = new mesh();
    c_h_mesh->iCount = 987;
    c_h_mesh->vCount = 123;
    return c_h_mesh;
}

//函数形参指针，参名后缀标识意图
//参名后缀 _out ，用作 传出值（ref）
EXPORT_API void pSuffixOut(int *val_out)
{
    *val_out = 951753;
}

//参名后缀 _arr ，用作 数组
EXPORT_API void pSuffixArr(int *val_arr, int arrLen, char *buf_arr)
{
    std::string str = "pSuffixArr=";
    for (int i = 0; i < arrLen; i++)
    {
        str.append(",");
        str.append(std::to_string(val_arr[i]));
    }

    if (strFun != nullptr)
    {
        srt2CharPtr(str, buf_arr);
        strFun((char16_t *)buf_arr);
    }
}

//参名后缀 _arr ，用作 句柄（地址ID）
EXPORT_API void pSuffixHandle(int *val_handle, char *buf_arr)
{
    c_h_int = val_handle;
    std::string str = "pSuffixHandle";
    if (strFun != nullptr)
    {
        srt2CharPtr(str, buf_arr);
        strFun((char16_t *)buf_arr);
    }
}

//函数返回值指针,类型别名标识意图
EXPORT_API num_Array retAliasArray(float *buf_arr, int bufLen, int *__retArrLen_out)
{
    *__retArrLen_out = bufLen;
    buf_arr[0] = 7;
    return buf_arr;
}

//---------------------

EXPORT_API float voidFun()
{
    printf("voidFun call");
    return 1.1f;
}

EXPORT_API int testPDefVal(int a, int b)
{
    return a + b;
}

EXPORT_API float addNum(float a, float b, float *_out)
{
    float result = a + b;
    *_out = result;
    printf("addNum: %f + %f = %f \n", a, b, result);
    return result;
}

SUB_EXPORT_API void *subNum(float a, float b, int *aa_out)
{
    float val = a - b;
    int result = (int)(val);
    *aa_out = result;
    printf("subNum: %f - %f = %f , %d \n", a, b, val, *aa_out);
    return aa_out;
}

EXPORT_API float mulNum(float a, float *b_arr, int arrSize)
{
    float result = a;
    printf("mulNum: %f", a);
    for (int i = 0; i < arrSize; i++)
    {
        result *= b_arr[i];
        printf("* %f", b_arr[i]);
    }
    printf("= %f  \n", result);
    return result;
}

EXPORT_API void printaddress(void *address, AddRessType _type)
{
    int *ip = (int *)address;
    float *fp = (float *)address;
    switch (_type)
    {
    case INT:
        printf("address : %d , val = %d \n", address, *ip);
        break;
    case FLOAT:
        printf("address : %d , val = %f \n", address, *fp);
        break;
    }
}

EXPORT_API char16_t *repeatStr(char16_t *str, char *buf_arr)
{
    std::string inStr = StringConv::ToUtf8String(str);
    int size = inStr.length();
    std::string outStr = inStr.substr(0, size / 2);
    outStr = outStr + outStr;
    auto outStr16 = StringConv::ToUtf16String(outStr);
    //同步字符串
    int min = size < outStr16.length() ? size : outStr16.length() - 1;
    for (int i = 0; i < min; i++)
    {
        str[i] = outStr16.c_str()[i];
    }
    if (min < size)
        str[min + 1] = 0;
    std::cout << "Str :" << inStr << " ,repeatStr:" << outStr << std::endl;

    //
    std::string bufStr = StringConv::ToUtf8String((char16_t *)buf_arr);
    std::cout << "buf_arr Str :" << bufStr << std::endl;
    buf_arr[4] = 'o';
    buf_arr[6] = 'k';

    return (char16_t *)buf_arr;
}
